'use strict';

const Controller = require('egg').Controller;

const jwt = require('jsonwebtoken');

class UserController extends Controller {
    //登录
    async login() {
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;

        if(username && password){
            let target = await service.user.login(username,password);
            if(target.length){
                //登录成功
                //生成token
                let token = jwt.sign({username},'lixd',{expiresIn:60*60*5});
                ctx.body = {
                    code:1,
                    msg:'登录成功',
                    token
                }
            }else{
                //登录失败
                ctx.body = {
                    code:0,
                    msg:'登录失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = UserController;
